from django import forms
from .models import Artical


class ArticalForm(forms.ModelForm):
    class Meta:
        model = Artical
        fields = ['title', 'category', 'tags', 'img', 'excerpt', 'body', 'recom']
        widgets = {
            'tags': forms.CheckboxSelectMultiple(),
            'body': forms.Textarea(attrs={'class': 'form-control', 'rows': 10}),
        }
    def clean_body(self):
        body = self.cleaned_data.get('body')
        # 使用 bleach 清理输入
        allowed_tags = ['a', 'b', 'i', 'u', 'strong', 'em', 'p', 'br']  # 根据需要自定义允许的标签
        allowed_attributes = {
            '*': [],
            'a': ['href', 'title'],
        }
        cleaned_body = clean(body, tags=allowed_tags, attributes=allowed_attributes)
        if not cleaned_body.strip():
            raise ValidationError("内容不能为空或仅包含空格。")
        return cleaned_body